package com.leejie.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
//在指定使用sqlSessionFactoryRef的情况下，这里有一个或多个的Spring的容器。经常我们会使用一个或多个的数据库.
@MapperScan(basePackages = {"com.leejie.mapper"},sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {

    @Bean(name = "db1DataSource")
    //error: Prefix must be in canonical form --- prefix里面不能用驼峰写法，全部小写
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    @Primary
    public DataSource HelloDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db2DataSource")
    //error: Prefix must be in canonical form --- prefix里面不能用驼峰写法，全部小写
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource HelloDataSource02(){
        return DataSourceBuilder.create().build();
    }

    @Bean
    public DynamicDataSource dataSource(@Qualifier("db1DataSource") DataSource db1DataSource,
                                        @Qualifier("db2DataSource") DataSource db2DataSource) {
        Map<Object, Object> map = new HashMap<>();
        map.put(DataSourceType.db1, db1DataSource);
        map.put(DataSourceType.db2, db2DataSource);

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(map);
        dynamicDataSource.setDefaultTargetDataSource(db1DataSource);

        return dynamicDataSource;
    }

    @Bean(value = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dynamicDataSource);
//        factoryBean.setTypeAliasesPackage();
        // 设置mapper.xml的位置路径
//        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml");
//        factoryBean.setMapperLocations(resources);
        return factoryBean.getObject();
    }







//    @Bean(name = "sqlSessionFactory01")
//    @Primary
//    @Qualifier("dataSource01")
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource01") DataSource dataSource01) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
////        bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/xxx/*.xml"));
//        bean.setDataSource(dataSource01);
//        return bean.getObject();
//    }
//
//
//
//
//    @Bean(name = "sqlSessionFactory01")
//    @Qualifier("dataSource02")
//    public SqlSessionFactory sqlSessionFactory01(@Qualifier("dataSource02") DataSource dataSource01) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
////        bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/xxx/*.xml"));
//        bean.setDataSource(dataSource01);
//        return bean.getObject();
//    }

}

















